//
// Created by dyc on 25-1-6.
//


#pragma once

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cstring>
#include <thread>
#include <param.h>
#include <camera.h>
#include "detector.h"


namespace hitcrt {
    class FPSCalculator {
    public:
        FPSCalculator() = default;

        void record() {
            timestamps.push_back(std::chrono::steady_clock::now());
            count++;
        };

        double calculate() {
            if (count > 1) {
                std::chrono::duration<double> totalDuration = {};
                int i = 1;
                while (i < count) {
                    totalDuration += timestamps[i] - timestamps[i - 1];
                    i += 1;
                }
                fps = 1 / (totalDuration.count() / (count - 1));
                count = 0;
            }
            return fps;
        }

        int count = 0;
        double fps = 0;

    private:
        std::vector<std::chrono::steady_clock::time_point> timestamps;
    };

    class Control {
    public:
        Control();

        ~Control() = default;

        void run();

        void show();

        void detect();

        Detect detector;
        FPSCalculator FPS;
        cv::Mat image;
        std::mutex mutex;

    private:
        Camera camera;
        int IF_CAMERA;
        int IF_SHOW_IMG;
        int IF_SHOW_MASK;
        int IF_DETECT;
        int SAVE_VIDEO;
        int IF_CALCULATE;
        std::string SAVE_PATH;
        std::string SAVE_NAME;
        std::string VIDEO_PATH;
        double VIDEO_FPS;
    };
}
